<?php

if (!isset($_COOKIE["checkLoginAdm"])) {
	
	header("Location: index.php");
}
?>
<?php

require("php/config.php");
require("php/postgres.php");

function validate_normal($value) {

	return preg_match("/[a-zA-Z ]+/", $value);
}

function validate_login($value) {

	return preg_match("/[a-zA-Z0-9_*!@#$%&() ]+/", $value);
}

function validate_pass($value) {

	return preg_match("/[a-zA-Z0-9_!@#$%&*()]+/", $value);
}

function validate_cpf($value) {

	return preg_match("/[0-9]{11,11}/", $value);
}
	

function validate_tel($value) {

	return preg_match("/[0-9]{8,11}/", $value);
}

try {
	
	$confs = $GLOBALS["CONFIGS"]["PSQL_DB"];
	$pgcon = new PostgresConnection($confs["HOST"], $confs["PORT"], $confs["USER"], $confs["PASSWORD"], $confs["DATABASE"]);
	
	$mand_fields = array(
						 	array("field" => "nome", "val_func" => "validate_normal"),
							array("field" => "ender", "val_func" => "validate_normal"),
							array("field" => "cpf", "val_func" => "validate_cpf"),
							array("field" => "login", "val_func" => "validate_login"),
							array("field" => "pass", "val_func" => "validate_pass"),
							array("field" => "tel", "val_func" => "validate_tel"),
						 );
	
	foreach ($mand_fields as $field) {
	
		if (empty($_POST[$field["field"]]) || !call_user_func($field["val_func"], $_POST[$field["field"]])) {
			
			echo $_POST[$field["field"]];
			echo $field["field"];
			die("2");
		}
	}
	
	if (!$pgcon->addUser($_POST)) {
	
		die("3");
	}
	
} catch (PostgresConnectionException $pce) {
}

echo "0";
?>